#!/bin/sh

# inotify-triggered file synchronization
# Copyright (c) 2020 Kaarle Ritvanen

. /etc/in-sync.conf

log() {
	logger -p daemon.$1 "$(basename "$0"): $2"
}

list_files() {
	local path
	cat /etc/in-sync.d/* | while read path; do
		[ -e "$path" ] && echo "$path"
	done
}

while :; do
	for host in $TARGET_HOSTS; do
		if list_files | rsync -avz --files-from - / $host:/; then
			log info "successfully synchronized to $host"
		else
			log err "synchronization to $host failed"
		fi
	done
	list_files | inotifywait -e close_write -t 900 --fromfile -
	[ $? -eq 1 ] && sleep 60
done
